<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="120px">
			<div class="app-space-between">
				<div class="app-space-between-left">

					<el-row :gutter="20">
						<el-col :span="8">
							<el-form-item label="商品名称:" prop="goodsName">
								<el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable
									@clear="handleQuery" @keyup.enter.native="handleQuery" />
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="商品类目:" prop="goodsCategoryId">
								<div style="width: 217px;">
									<treeselect v-model="queryParams.goodsCategoryId" :options="menuOptions"
										:normalizer="normalizer" :show-count="true" placeholder="主类目" />
								</div>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="上下架:" prop="productIntroduction">
								<el-select v-model="queryParams.productIntroduction" placeholder="请选择上下架"
									@change="handleQuery" clearable>
									<el-option label="上架" value="1" />
									<el-option label="下架" value="0" />
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>


					<el-row :gutter="20">
						<el-col :span="8">
							<el-form-item label="商品分类:" prop="goodsTypeId">
								<div style="width: 217px;">
									<treeselect v-model="queryParams.goodsTypeId" :options="menuOptionsType"
										:normalizer="normalizerType" :show-count="true" placeholder="主类目" />
								</div>
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="供应商:" prop="supplier">
								<el-input v-model="queryParams.supplier" placeholder="请输入供应商" clearable
									@clear="handleQuery" @keyup.enter.native="handleQuery" />
							</el-form-item>
						</el-col>
						<el-col :span="8">

							<el-form-item label="合作方式:" prop="cooperationMethods">
								<el-select v-model="queryParams.cooperationMethods" placeholder="请选择合作方式"
									@change="handleQuery" clearable>
									<el-option label="采购" value="采购" />
									<el-option label="代销" value="代销" />
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>

					<el-row :gutter="20">
						<el-col :span="12">
							<el-form-item label="商品上/下架时间:" prop="selectorTime">
								<el-date-picker v-model="queryParams.selectorTime" @change="handleQuery"
									type="datetimerange" range-separator="至" start-placeholder="上架时间"
									end-placeholder="下架时间">
								</el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="价格:">
								<el-input placeholder="请输入" v-model="queryParams.fromPrice" style="width: 140px;">
									<template slot="append">元</template>
								</el-input>
								<span style="color: #cccccc;"> —— </span>
								<el-input placeholder="请输入" v-model="queryParams.lastPrice" style="width: 140px;">
									<template slot="append">元</template>
								</el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
				<div class="app-space-between-right">
					<el-form-item>
						<el-button type="primary" size="mini" @click="handleQuery">搜索</el-button>
						<el-button size="mini" @click="resetQuery">重置</el-button>
					</el-form-item>
				</div>
			</div>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleAdd">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="primary" plain size="mini" @click="handleUpAndDown">上下架</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
			<el-table-column show-overflow-tooltip type="selection" width="55" align="center" />
			<el-table-column show-overflow-tooltip label="商品名称" width="180" align="center" prop="goodsName" />
			<el-table-column show-overflow-tooltip label="商品价格" align="center" prop="mealCardPay"></el-table-column>
			<el-table-column show-overflow-tooltip label="线上可售数量" align="center" prop="ispost" />
			<el-table-column show-overflow-tooltip label="占用数量" align="center" prop="postage"></el-table-column>
			<el-table-column show-overflow-tooltip label="实际销售数量" align="center" prop="newProduct"></el-table-column>
			<el-table-column show-overflow-tooltip label="上下架状态" align="center" prop="productIntroduction">
				<template slot-scope="scope">
					{{scope.row.productIntroduction == '0' ? '下架' : '上架'}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="上架时间" align="center">
				<template slot-scope="scope">
					{{scope.row.fromOnshowTime | $YYYY_MM_DD_hh_mm_ss}}
				</template>
			</el-table-column>
			<el-table-column show-overflow-tooltip label="排序" align="center" prop="sort"></el-table-column>
			<el-table-column show-overflow-tooltip label="操作" align="center" width="140" fixed="right">
				<template slot-scope="scope">
					<el-link type="primary" @click="handleUpdate(scope.row)" :underline="false">编辑</el-link>
					<el-link type="primary" @click="handleDelete(scope.row)" :underline="false" style="margin:0 6px;">删除
					</el-link>
					<el-link type="primary" :underline="false" @click="handleUpAndDown(scope.row)">上下架</el-link>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 增 改 -->
		<shoppingAdd ref="shopping-add" :classificationData="classificationData" @success="handleQuery"></shoppingAdd>
		<!-- 上下架 -->
		<upAndDown ref="up-and-down" @success="handleQuery"></upAndDown>
		<!--  -->
	</div>
</template>

<script>
	import {
		listWcGoods,
		deleteWcGoods
	} from "@/api/culturalCreativity/shopping.js";
	import {
		listWcGoodsCategory
	} from "@/api/culturalCreativity/category.js";
	import {
		listWcGoodsType
	} from "@/api/culturalCreativity/classification.js";
	import shoppingAdd from "./components/shopping-add.vue";
	import upAndDown from "./components/up-and-down.vue";
	import Treeselect from "@riophae/vue-treeselect";
	import "@riophae/vue-treeselect/dist/vue-treeselect.css";
	export default {
		components: {
			shoppingAdd,
			upAndDown,
			Treeselect
		},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 岗位表格数据
				postList: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					goodsName: "",
					goodsCategoryId: "",
					goodsTypeId: "",
					supplier: "",
					cooperationMethods: "",
					fromPrice: "",
					lastPrice: "",
					productIntroduction: "",
					selectorTime: []
				},
				menuOptions: [],
				menuOptionsType: [],
				classificationData: [],
			};
		},
		async created() {
			await this.getLeiMu();
			await this.getLeiMuType();
			let res1 = await listWcGoodsType({}, {
				pageNum: 1,
				pageSize: 9999,
			});
			this.classificationData = res1.rows;
			this.getList();
		},
		methods: {
			/** 查询岗位列表 */
			getList() {
				this.loading = true;
				let queryParams = {
					...this.queryParams
				};
				queryParams.fromOnshowTime = queryParams.selectorTime[0] || '';
				queryParams.lastOnshowTime = queryParams.selectorTime[1] || '';

				delete queryParams.selectorTime;
				listWcGoods({},queryParams).then(response => {
					this.postList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			async getLeiMu() {
				this.menuOptions = [];
				let res = await listWcGoodsCategory({}, {
					pageNum: 1,
					pageSize: 99999,
				});
				const menu = {
					goodsCategoryId: '',
					goodsCategoryName: '主类目',
					children: []
				};
				menu.children = this.handleTree(res.rows, "goodsCategoryId", "goodsCategoryParentId");
				this.menuOptions.push(menu);
				return;
			},
			normalizerType(node) {
				if (node.children && !node.children.length) {
					delete node.children;
				}
				return {
					id: node.goodsTypeId,
					label: node.goodsTypeName,
					children: node.children
				};
			},
			/** 转换菜单数据结构 */
			normalizer(node) {
				if (node.children && !node.children.length) {
					delete node.children;
				}
				return {
					id: node.goodsCategoryId,
					label: node.goodsCategoryName,
					children: node.children
				};
			},
			handleUpAndDown(row) {
				let postIds = row.goodsId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString();
				this.$refs['up-and-down'].init(postIds);
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.queryParams.fromPrice = "";
				this.queryParams.lastPrice = "";
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.goodsId)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.$refs['shopping-add'].init();
			},
			async getLeiMuType() {
				this.menuOptionsType = [];
				let res = await listWcGoodsType({}, {
					pageNum: 1,
					pageSize: 99999,
				});
				const menu = {
					goodsTypeId: '',
					goodsTypeName: '主类目',
					children: []
				};
				menu.children = this.handleTree(res.rows, "goodsTypeId", "modifyOperatorId");
				this.menuOptionsType.push(menu);
				return;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				let postId = row.goodsId || this.ids
				if (Array.isArray(postId)) postId = postId.toString();
				this.$refs['shopping-add'].init(postId);
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				let postIds = row.goodsId || this.ids;
				if (Array.isArray(postIds)) postIds = postIds.toString()
				this.$modal.confirm('此操作将要删除所选的数据，是否继续？').then(function() {
					return deleteWcGoods(postIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		}
	};
</script>
